package day12;

//volatile重排序
public class Demo3 {
    static int a, b, c, d;

    public static void main(String[] args) throws InterruptedException {
        int i = 0;  //计数的
        while (true) {
            i++;
            a = b = c = d = 0;   //每执行一次 数据清零
            Thread t1 = new Thread(() -> {
                a = 1;
                c = b;
            });
            Thread t2 = new Thread(() -> {
                b = 1;
                d = a;
            });
            t1.start();
            t2.start();

            t1.join();
            t2.join();
            if (c == 0 && d == 0) { //重排序了
                System.out.println("执行了第" + i + "次出现了重排序");
            } else {
                System.out.println(i);
            }
        }
    }
}